
#include "ndicontrol.h"
#include "CommandHandling.h"
#include "INIFileRW.h"
#include <fstream>
CNDIControl::CNDIControl(void)
{
	m_pCmdHandling = new CCommandHandling;
	m_bWireless = true;
	m_bSystemInitialized = false;
	m_IsTracking = false;
	m_nTrackingMode = 0;
}
void CNDIControl::StartTracking(void)
{
	if (m_IsTracking)
		return;
	m_pCmdHandling->nStartTracking();
	m_IsTracking = true;
}
void CNDIControl::StopTracking(void)
{
	if (!m_IsTracking)
		return;
	m_pCmdHandling->nStopTracking();
	m_IsTracking = false;
}
bool CNDIControl::StartInitialize(void)
{
	
	if (InitializeSystem())
	{
		if (m_pCmdHandling->nActivateAllPorts())
		{
			m_bSystemInitialized = true;
			return true;
		}
	}
	return false;
	
}
CNDIControl::~CNDIControl(void)
{
	if (m_IsTracking)
		StopTracking();

	delete m_pCmdHandling;
}

int CNDIControl::InitializeSystem(void)
{
	int
		nBaudRate = 0,
		nStopBits = 0,
		nParity   = 0,
		nDataBits = 0,
		nHardware = 0;
	CCommandHandling *pCommandHandling = m_pCmdHandling;


	/*
	* read the COM port parameters from the ini file
	*/
	ReadINIParm( "Communication", "Baud Rate", "0", sizeof(nBaudRate), &nBaudRate );
	ReadINIParm( "Communication", "Stop Bits", "0", sizeof(nStopBits), &nStopBits );
	ReadINIParm( "Communication", "Parity", "0", sizeof(nParity), &nParity );
	ReadINIParm( "Communication", "Data Bits", "0", sizeof(nDataBits), &nDataBits );
	ReadINIParm( "Communication", "Hardware", "0", sizeof(nHardware), &nHardware );
	ReadINIParm( "Communication", "COM Port", "0", sizeof(m_nCOMPort), &m_nCOMPort );

	/* 
	* This feature is useful for debugging only, m_bResetHardware is set to 
	* TRUE to disable it. 
	*/

	ReadINIParm( "Communication", "Reset Hardware", "0", 2, &m_bResetHardware );
	m_bResetHardware = TRUE;

	/*
	* close, then open the port
	*/

	pCommandHandling->nCloseComPorts();

	if (!pCommandHandling->nOpenComPort( m_nCOMPort ))
	{
		MessageBox( NULL, "COM Port could not be opened.  "
			"Check your COM Port settings and\n"
			"make sure you system is turned on.",
			"COM ERROR", MB_ICONERROR|MB_SYSTEMMODAL|MB_SETFOREGROUND );
		return 0;
	} /* if */

	/*
	* if we are supposed to reset, call the reset now
	*/
	if ( m_bResetHardware )
	{
		
		if (!pCommandHandling->nHardWareReset())
			return 0;
	}/* if */

	/*
	* set the System COM Port parameters, then the computers COM Port parameters.
	* if that is successful, initialize the system
	*/
	if(pCommandHandling->nSetSystemComParms( nBaudRate, nDataBits, nParity, nStopBits, nHardware ))
	{
		if(pCommandHandling->nSetCompCommParms( nBaudRate, nDataBits, nParity, nStopBits, nHardware ))
		{
			if(pCommandHandling->nInitializeSystem())
			{
				/*
				* get the system information
				*/
				if (!pCommandHandling->nGetSystemInfo())
				{
					/*
					* Check system type: Polaris, Polaris Accedo, and Aurora
					*/
					MessageBox(NULL, "Could not determine type of system\n"
						"(POLARIS, POLARIS ACCEDO or AURORA)\n"
						"Please contact Northern Digital Inc. for assistance",
						"INIT ERROR", MB_ICONERROR|MB_SYSTEMMODAL|MB_SETFOREGROUND );
					return 0;
				} /* if */

				/* 
				* Set firing rate if system type is Polaris or Polaris Accedo.
				*/
				if ( (pCommandHandling->m_dtSystemInformation.nTypeofSystem == POLARIS_SYSTEM ) ||
					(pCommandHandling->m_dtSystemInformation.nTypeofSystem == ACCEDO_SYSTEM))
				{
					pCommandHandling->nSetFiringRate(); 
				}/* if */

				m_bSystemInitialized = TRUE;

				//MessageBox(NULL, "System successfully intialized", "Initialization", MB_ICONINFORMATION|MB_SYSTEMMODAL|MB_SETFOREGROUND );
				return 1;
			} /* if */
			else
			{
				MessageBox( NULL, "System could not be initialized. "
					"Check your COM Port settings, make sure your\n"
					"system is turned on and the system components are compatible.",
					"INIT ERROR", MB_ICONERROR|MB_SYSTEMMODAL|MB_SETFOREGROUND );
			}/* else */
		}/* if */
	}/* if */
	return 0;
}
